package com.czl.module_service.adapter

import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.EditText
import android.widget.ExpandableListAdapter
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.base.BaseFragment
import com.czl.base.base.BaseRxFragment
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.data.bean.tengyun.*
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemAssetHandleApplySortCardBinding
import com.czl.module_service.databinding.ItemAssetHandleCardInfoBinding
import com.czl.module_service.databinding.ItemAssetHandleCardInfoChildBinding
import com.czl.module_service.databinding.ItemAssetHandleCardInfoParentBinding
import com.czl.module_service.fragment.assetHandle.AssetHandleApplyAddFragment
import com.czl.module_service.view.ListSheetDialogFragment

class AssetHandleApplyCardLowerInfoAdapter (private val fragment: BaseRxFragment) :
    BaseExpandableListAdapter() {

/*    override fun convert(
        holder: BaseDataBindingHolder<ItemAssetHandleCardInfoBinding>,
        item: AssetHandleFacilityBean,
    ) {
        holder.dataBinding?.run {
            data = item
            adapter = this@AssetHandleApplyCardInfoAdapter
            executePendingBindings()
        }
    }*/

//    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
//
//    })

    var mdata: MutableList<LowerListBean> = arrayListOf()
    var isEdit: Boolean? = false

    fun setListData(temp: MutableList<LowerListBean>) {
//        mdata = temp
        setListData(temp,false)
    }
    fun setListData(temp: MutableList<LowerListBean>,p0: Boolean) {
        mdata = temp
        isEdit = p0
        notifyDataSetChanged()
    }

    override fun getGroupCount(): Int {
        return mdata!!.size
    }

    override fun getChildrenCount(p0: Int): Int {
        if (mdata.isEmpty()) {
            return 0
        }
        val sortList = mdata!!.get(p0).sortList
        return sortList.size
    }

    override fun getGroup(p0: Int): Any {
        return mdata!!.get(p0)
    }

    override fun getChild(p0: Int, p1: Int): Any {
        val sortList = mdata!!.get(p0).sortList
        return sortList.get(p1)
    }

    override fun getGroupId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getChildId(p0: Int, p1: Int): Long {
        return p1.toLong()
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getGroupView(p0: Int, p1: Boolean, p2: View?, p3: ViewGroup?): View {
//        var binding = ItemAssetHandleCardInfoParentBinding.inflate(LayoutInflater.from(p3?.context),p3,false)
        var binding: ItemAssetHandleCardInfoParentBinding
        var view = p2
        if (view == null) {
            binding = ItemAssetHandleCardInfoParentBinding.inflate(LayoutInflater.from(p3?.context),p3,false)
            view = binding?.root
            view.tag = binding
        } else{
            binding = view.getTag() as ItemAssetHandleCardInfoParentBinding
        }
        val lowerBean = mdata.get(p0)
        binding.tvLoc.setText(lowerBean.note)
        if (p1) {
            binding.ivDown.setImageDrawable(ContextCompat.getDrawable(p3!!.getContext(), R.drawable.ic_baseline_keyboard_arrow_down_24));
        } else {
            binding.ivDown.setImageDrawable(ContextCompat.getDrawable(p3!!.getContext(), R.drawable.ic_baseline_keyboard_arrow_up_24));
        }
        return view
    }

    override fun getChildView(p0: Int, p1: Int, p2: Boolean, p3: View?, p4: ViewGroup?): View {
//        var binding = ItemAssetHandleCardInfoChildBinding.inflate(LayoutInflater.from(p4?.context),p4,false)
        if (isEdit!!) {
            return getChildEditView(p0, p1, p2, p3, p4)
        }
        var binding: ItemAssetHandleCardInfoChildBinding
        var view = p3
        if (view == null) {
            binding = ItemAssetHandleCardInfoChildBinding.inflate(LayoutInflater.from(p4?.context),p4,false)
            view = binding?.root
            view.tag = binding
        } else{
            binding = view.getTag() as ItemAssetHandleCardInfoChildBinding
        }
        var lowerBean = mdata.get(p0)
        var sortBean = lowerBean.sortList.get(p1)
        if (lowerBean.manageMode.equals(1)) {
            binding.tvSortNum.visibility = View.GONE
            binding.tvSortCode.setText("编码：" + sortBean.sortNo)
        } else {
            binding.tvSortCode.visibility = View.GONE
            binding.tvSortNum.setText("数量：" + sortBean.assetCount)
        }
        if (lowerBean.inuse.equals(0)) {
            binding.tvLocatInfo.setText("位置：" + sortBean.locationDesc)
        } else {
            binding.tvLocatInfo.visibility = View.GONE
        }
        binding.etSortName.setText(sortBean.sortName)
        binding.tvSortModel.setText("规格：" + sortBean.model)
        binding.etHandle.setText("拟处置方式：" + sortBean.getHandleResultStr())
        binding.etReason.setText("处置原因：" + sortBean.reason)
        return view
    }
    fun getChildEditView (p0: Int, p1: Int, p2: Boolean, p3: View?, p4: ViewGroup?): View {
//        var binding = ItemAssetHandleCardInfoChildBinding.inflate(LayoutInflater.from(p4?.context),p4,false)
        var binding: ItemAssetHandleApplySortCardBinding
        var view = p3
        if (view == null) {
            binding = ItemAssetHandleApplySortCardBinding.inflate(LayoutInflater.from(p4?.context),p4,false)
            view = binding?.root
            view.tag = binding
        } else{
            binding = view.getTag() as ItemAssetHandleApplySortCardBinding
        }
        var lowerBean = mdata.get(p0)
        var sortBean = lowerBean.sortList.get(p1)
        if (lowerBean.manageMode.equals(1)) {
            binding.tvSortNum.visibility = View.GONE
            binding.tvSortCode.setText("编码：" + sortBean.sortCode)
        } else {
            binding.tvSortCode.visibility = View.GONE
            binding.tvSortNum.setText("数量：" + sortBean.assetCount)
        }
        if (lowerBean.inuse.equals(0)) {
            binding.tvLocatInfo.setText("位置：" + sortBean.locationDesc)
        } else {
            binding.tvLocatInfo.visibility = View.GONE
        }
        binding.etSortName.setText(sortBean.sortName)
        binding.tvSortModel.setText("规格：" + sortBean.sortModel)
        binding.etReason.setText(sortBean.reason)
        var planAssetTypeTemp = "请选择拟处置方式"
        if (sortBean.planAssetType != null) {
            planAssetTypeTemp = sortBean.getHandleResultStr()
        }
        binding.stvPlanAssetType.setRightText(planAssetTypeTemp)
        binding.stvPlanAssetType.setOnClickListener {
            ListSheetDialogFragment.showBottomSheet(
                fragment.parentFragmentManager,
                HandleResultList
            ) {
                sortBean.planAssetType = it
                sortBean.planAssetTypeTemp = HandleResultList[it]
                binding.stvPlanAssetType.setRightText(HandleResultList[it])
//                setData(sortBean.position,sortBean)
            }
        }
        setEditTextTag(binding.etReason,sortBean)
        binding.deleteImg.setOnClickListener {
            if (lowerBean.sortList.size == 1){
                mdata.removeAt(p0)
            } else {
                lowerBean.sortList.removeAt(p1)
            }
            notifyDataSetChanged()
            if (fragment is AssetHandleApplyAddFragment) {
                fragment.deleteSortInfo(sortBean)
            }
        }
//        binding.etHandle.setText("拟处置方式：" + sortBean.getHandleResultStr())
//        binding.etReason.setText("处置原因：" + sortBean.reason)
        return view
    }

    fun setEditTextTag(editText: EditText,  sortBean: AssetHandleSortBean) {
        if (editText.tag != null && editText.tag is TextWatcher) {
            editText.removeTextChangedListener(editText.tag as TextWatcher)
        }
        editText.setText(sortBean.reason)
        val textWatcher: TextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(editable: Editable) {
                sortBean.reason = editable.toString()
            }
        }
        editText.addTextChangedListener(textWatcher)
        editText.tag = textWatcher
    }

    override fun isChildSelectable(p0: Int, p1: Int): Boolean {
        return true
    }
}
